// sort.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "cv.h"
#include <fstream>
#include <iostream>
using namespace std;
#define thresholdSort 2
#define meetSortX(vv) \
	abs(vv[0].x-vv[1].x)>thresholdSort&&abs(vv[1].x-vv[2].x)>thresholdSort
#define meetSortY(vv) \
	abs(vv[0].y-vv[1].y)>thresholdSort&&abs(vv[1].y-vv[2].y)>thresholdSort
#define thresholdSort 0.001
#define meetSort3X(vv) \
	abs(vv[0].x-vv[1].x)>thresholdSort&&abs(vv[1].x-vv[2].x)>thresholdSort
#define meetSort3Y(vv) \
	abs(vv[0].y-vv[1].y)>thresholdSort&&abs(vv[1].y-vv[2].y)>thresholdSort
#define euildistence(a,b) ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))
#define euildistence3(a,b) ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z))
#define euilsort(vv) \
	if(euildistence(vv[0],vv[1])>euildistence(vv[1],vv[2])) \
	swap(vv[0],vv[2])
#define euilsort3(vv) \
	if(euildistence3(vv[0],vv[1])>euildistence3(vv[1],vv[2])) \
	swap(vv[0],vv[2])
bool sortcoodx(CvPoint2D32f& m1,CvPoint2D32f& m2){ return m1.x<m2.x;}
bool sortcoody(CvPoint2D32f& m1,CvPoint2D32f& m2){ return m1.y<m2.y;}
bool sortcood3x(CvPoint3D32f& m1,CvPoint3D32f& m2){ return m1.x<m2.x;}
bool sortcood3y(CvPoint3D32f& m1,CvPoint3D32f& m2){ return m1.y<m2.y;}
void sort2D(string prx,string instr,string outstr)
{
	ifstream in((prx+instr).c_str());
	vector<float> sortv;
	vector<float> sortvv;
	string line;
   while (getline(in,line))
    {
		istringstream sstr(line);
		int num;
		sstr>>num;
		if(num==3)
          copy(istream_iterator<float>(sstr),istream_iterator<float>(),back_inserter(sortv));
		else
		{
			for(int i=0;i<6;i++)
				sortv.push_back(-1);
		}
    } ;
	in.close();in.clear();
	for (int i=0;i<sortv.size()/6;i++)
	{
		vector<CvPoint2D32f> tempCVP;
		for(int j=0;j<3;j++)
			tempCVP.push_back(cvPoint2D32f(sortv[i*6+j*2],sortv[i*6+j*2+1]));
		if(sortv[i*6]!=-1)
		{
		if (meetSortX(tempCVP))
			sort(tempCVP.begin(),tempCVP.end(),sortcoodx);
		else if(meetSortY(tempCVP))
			sort(tempCVP.begin(),tempCVP.end(),sortcoody);
		euilsort(tempCVP);
		}
		for(int i=0;i<3;i++)
		{
			sortvv.push_back(tempCVP[i].x);
			sortvv.push_back(tempCVP[i].y);
		}
	}
	ofstream out((prx+outstr).c_str());
	for (int i=0;i<sortvv.size()/2;i++)
		out<<sortvv[2*i]<<" "<<sortvv[2*i+1]<<endl;
	out.close();
	out.clear();
}
void sort3D(string prx,string instr,string outstr)
{
	ifstream in((prx+instr).c_str());
	vector<float> sortv;
	vector<float> sortvv;
	copy(istream_iterator<float>(in),istream_iterator<float>(),back_inserter(sortv));
	in.close();in.clear();
	for (int i=0;i<sortv.size()/9;i++)
	{
		vector<CvPoint3D32f> tempCVP;
		for(int j=0;j<3;j++)
			tempCVP.push_back(cvPoint3D32f(sortv[i*9+j*3],sortv[i*9+j*3+1],sortv[i*9+j*3+2]));
		if (meetSort3X(tempCVP))
			sort(tempCVP.begin(),tempCVP.end(),sortcood3x);
		else if(meetSort3Y(tempCVP))
			sort(tempCVP.begin(),tempCVP.end(),sortcood3y);
 		euilsort3(tempCVP);
		for(int i=0;i<3;i++)
		{
			sortvv.push_back(tempCVP[i].x);
			sortvv.push_back(tempCVP[i].y);
			sortvv.push_back(tempCVP[i].z);
		}
	}
	ofstream out((prx+outstr).c_str());
	for (int i=0;i<sortvv.size()/3;i++)
		out<<sortvv[3*i]<<" "<<sortvv[3*i+1]<<" "<<sortvv[3*i+2]<<endl;
	out.close();
	out.clear();
}
int _tmain(int argc, _TCHAR* argv[])
{
/*	sort3D(".\\data\\","3d.txt","3do.txt");*/
	sort2D(".\\data\\","points.00.txt","2d1.txt");
	return 0;
}

